

package com.nn.smart.bus.controller;

import com.nn.smart.bus.commons.tools.annotation.LogOperation;
import com.nn.smart.bus.commons.tools.page.PageData;
import com.nn.smart.bus.commons.tools.utils.ExcelUtils;
import com.nn.smart.bus.commons.tools.utils.Result;
import com.nn.smart.bus.commons.tools.validator.AssertUtils;
import com.nn.smart.bus.commons.tools.validator.ValidatorUtils;
import com.nn.smart.bus.commons.tools.validator.group.AddGroup;
import com.nn.smart.bus.commons.tools.validator.group.UpdateGroup;
import com.nn.smart.bus.commons.tools.validator.group.DefaultGroup;
import com.nn.smart.bus.dto.NndzCarDictDTO;
import com.nn.smart.bus.excel.NndzCarDictExcel;
import com.nn.smart.bus.service.NndzCarDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 定制模块字典表
 *
 * @author nanning nanning@123.com
 * @since v1.0.0 2021-08-25
 */
@RestController
@RequestMapping("nndzcardict")
public class NndzCarDictController {

    @Autowired
    private NndzCarDictService nndzCarDictService;

    @GetMapping("page")
    public Result<PageData<NndzCarDictDTO>> page(@RequestParam Map<String, Object> params){
        PageData<NndzCarDictDTO> page = nndzCarDictService.page(params);
        return new Result<PageData<NndzCarDictDTO>>().ok(page);
    }

    @GetMapping("sublist/{pid}")
    public Result<List<NndzCarDictDTO>> sublist(@PathVariable("pid") String pid) {
        Map map = new HashMap();
        map.put("pid", pid);
        List<NndzCarDictDTO> list = nndzCarDictService.subList(map);
        return new Result<List<NndzCarDictDTO>>().ok(list);
    }

    @GetMapping("list")
    public Result<List<NndzCarDictDTO>> list(@RequestParam Map<String, Object> params) {
        //字典分类数据
        List<NndzCarDictDTO> list = nndzCarDictService.list(params);

        return new Result<List<NndzCarDictDTO>>().ok(list);
    }

    @GetMapping("{id}")
    public Result<NndzCarDictDTO> get(@PathVariable("id") String id){
        NndzCarDictDTO data = nndzCarDictService.get(id);
        return new Result<NndzCarDictDTO>().ok(data);
    }

    @PostMapping
    public Result save(@RequestBody NndzCarDictDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        nndzCarDictService.save(dto);
        return new Result();
    }

    @PutMapping
    public Result update(@RequestBody NndzCarDictDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        nndzCarDictService.update(dto);
        return new Result();
    }

    @DeleteMapping
    public Result delete(@RequestBody String[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        nndzCarDictService.delete(ids);
        return new Result();
    }

    @GetMapping("export")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<NndzCarDictDTO> list = nndzCarDictService.list(params);
        ExcelUtils.exportExcelToTarget(response, null, list, NndzCarDictExcel.class);
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable String id) {
        //效验数据
        nndzCarDictService.delete(id);
        return new Result();
    }

    /**
     * 根据数据字典类型获取简版数据字典列表，用于页面下拉菜单
     *
     * @param dictType 数据字典类型
     * @return com.elink.esua.commons.tools.utils.Result<java.util.List < com.elink.esua.dto.SysSimpleDictDTO>>
     * @author wgf
     * @date 2021/8/26 09:33
     */
    @GetMapping("listSimple/{dictType}")
    public Result<List<NndzCarDictDTO>> listSimpleDictInfo(@PathVariable("dictType") String dictType) {
        //字典分类数据
        return nndzCarDictService.listSimpleDictInfo(dictType);
    }

}
